import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testlimitrange:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/1_get_limitrange_list.yaml'))
    def test_get_limitrange_list(self, limitrangeinfo):
        # 获取limitrange列表接口-正常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = base_url + limitrangeinfo['request']['url'].replace('{namespace}', 'default')
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/1_get_limitrange_list_exception.yaml'))
    def test_get_limitrange_list_exception(self, limitrangeinfo):
        # 获取limitrange列表接口-异常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = base_url + limitrangeinfo['request']['url'].replace('{namespace}', 'default')
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/limitrange/2_create_limitrange.yaml')
                             )
    def test_create_limitrange(self, limitrangeinfo):
        # 创建limitrange接口-正常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = base_url + limitrangeinfo['request']['url'].replace('{namespace}', 'default')
        method = limitrangeinfo['request']['method']
        data = limitrangeinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        limitrangename = req.json()['metadata']['name']
        globals()['limitrangename'] = limitrangename
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/2_create_limitrange_exception.yaml'))
    def test_create_limitrange_exception(self, limitrangeinfo):
        # 创建limitrange接口-异常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = base_url + limitrangeinfo['request']['url'].replace('{namespace}', 'default')
        method = limitrangeinfo['request']['method']
        data = limitrangeinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/3_get_specific_limitrange.yaml'))
    def test_get_specific_limitrange(self, limitrangeinfo):
        # 获取指定limitrange接口-正常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/3_get_specific_limitrange_exception.yaml'))
    def test_get_specific_limitrange_exception(self, limitrangeinfo):
        # 获取指定limitrange接口-异常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/limitrange/4_update_limitrange.yaml'))
    def test_update_limitrange(self, limitrangeinfo):
        # 更新指定limitrange接口-正常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        limitrangeinfo['request']['data'] = globals()['updatereqbody']
        data = limitrangeinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/4_update_limitrange_exception.yaml'))
    def test_update_limitrange_exception(self, limitrangeinfo):
        # 更新指定limitrange接口-异常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        limitrangeinfo['request']['data'] = globals()['updatereqbody']
        data = limitrangeinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/5_update_section_limitrange.yaml'))
    def test_update_section_limitrange(self, limitrangeinfo):
        # 部分更新指定limitrange的接口-正常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        data = limitrangeinfo['request']['data']
        header = limitrangeinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/5_update_section_limitrange_exception.yaml'))
    def test_update_section_limitrange_exception(self, limitrangeinfo):
        # 部分更新指定limitrange的接口-异常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        data = limitrangeinfo['request']['data']
        header = limitrangeinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/6_delete_specific_limitrange.yaml'))
    def test_delete_specific_limitrange(self, limitrangeinfo):
        # 删除limitrange接口-正常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.limitrange
    @pytest.mark.parametrize('limitrangeinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/limitrange/6_delete_specific_limitrange_exception.yaml'))
    def test_delete_specific_limitrange_exception(self, limitrangeinfo):
        # 删除limitrange接口-异常
        base_url = datas.baseUrl + Testlimitrange.url_unify
        url = (base_url + limitrangeinfo['request']['url']
               .replace('{namespace}', 'default').replace('{limitrangename}', globals()['limitrangename']))
        method = limitrangeinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == limitrangeinfo['validate'][0]['equals']['status_code']
